package com.xx.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xx.entity.Task;
import com.xx.util.DBHelp;
import com.xx.util.RowMapper;

public class TaskDao {
DBHelp<Task> dbHelp= new DBHelp<Task>();
public void insert(Task task){
	String sql="insert into t_task(id,name,`desc`,state,level,begintime,endtime,createtime,t_employee_id,t_goal_id,rate) value(?,?,?,?,?,?,?,?,?,?,?)";
	dbHelp.executeSQL(sql,task.getId(),task.getName(),task.getDesc(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getCreatetime(),task.getEmployeeid(),task.getGoalid(),task.getRate());
	
	
}

public List<Task> findlisTasks(String gid){
	String sql="select id,name,`desc`,state,level,begintime,endtime,createtime,rate,t_employee_id,t_goal_id from t_task where t_goal_id=?";
	return dbHelp.executeQueryForList(sql, new TaskMapper(), gid);
	
}	
	public class TaskMapper implements RowMapper<Task>{

		public Task mapperRow(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			Task task=new Task();
			task.setId(rs.getString("id"));
			task.setName(rs.getString("name"));
			task.setDesc(rs.getString("desc"));
			task.setState(rs.getString("state"));
			task.setLevel(rs.getString("level"));
			task.setBegintime(rs.getString("begintime"));
			task.setEndtime(rs.getString("endtime"));
			task.setCreatetime(rs.getString("createtime"));
			task.setEmployeeid(rs.getString("t_employee_id"));
			task.setGoalid(rs.getString("t_goal_id"));
			task.setRate(rs.getInt("rate"));
			LoginDao loginDao=new LoginDao();
			task.setEmployee(loginDao.findObject(rs.getString("t_employee_id")));

			return task;
			
		}}
	public Task findTask(String taskid) {
	String sql="select id,name,`desc`,rate,state,level,begintime,endtime,createtime,t_employee_id,t_goal_id from t_task where id=?";
		return dbHelp.executeQueryForObject(sql, new TaskMapper(), taskid);
	}

	public void update(Task task) {
	String sql="update t_task set name=?,`desc`=?,rate=?,state=?,level=?,begintime=?,endtime=? where id=? ";
		dbHelp.executeSQL(sql, task.getName(),task.getDesc(),task.getRate(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getId());
	}

	public void updates(Task task) {
		// TODO Auto-generated method stub
		String sql="update t_task set name=?,`desc`=?,rate=?,state=?,level=?,begintime=?,endtime=?,t_employee_id=? where id=? ";
		dbHelp.executeSQL(sql, task.getName(),task.getDesc(),task.getRate(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getEmployeeid(),task.getId());
	}

	public List<Task> findtaskById(String id,String pid) {
		// TODO Auto-generated method stub
	String sql="select id,name,`desc`,rate,state,level,begintime,endtime,createtime,t_employee_id,t_goal_id from t_task where t_employee_id=? and t_goal_id in(select id from t_goal where t_project_id=?) ";
	return dbHelp.executeQueryForList(sql, new TaskMapper(), id,pid);
	
	}

	public void dels(String gid) {
		String sql="delete from t_task where t_goal_id =?";
		dbHelp.executeSQL(sql, gid);
		
	}

	public void del(String taskid) {
		// TODO Auto-generated method stub
		String sql="delete from t_task where id =?";
		dbHelp.executeSQL(sql, taskid);
		
	}
	
}
